package com.sharedlease.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sharedlease.Feign.IndentClient;
import com.sharedlease.Feign.ProductClient;
import com.sharedlease.entity.*;
import com.sharedlease.service.MerchantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴钟富
 * @since 2021-03-02
 */
@RestController
@Api(tags = "厂商相关接口")
@RequestMapping("/merchant")
public class MerchantController {
    @Autowired
    public MerchantService merchantService;
    @Autowired
    public ProductClient productClient;
    @Autowired
    public IndentClient indentClient;
    @GetMapping("/getById/{id}")
    @ApiOperation("根据一个id查询厂商")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要查询的厂商id", defaultValue = "1"),

    }
    )
    public Merchant getById(@PathVariable Long id){
        Merchant merchant = merchantService.getById(id);
        return merchant;
    }

    @GetMapping("/list")
    @ApiOperation("查询全部厂商")
    public List<Merchant> list(){
        List<Merchant> list = merchantService.list(null);
        return list;
    }

    @PostMapping("/save")
    @ApiOperation("新增一个厂商")
    public R save(@RequestBody Merchant merchant) {
        String encodePassword= Base64Utils.encodeToUrlSafeString(merchant.getPassword().getBytes());
        merchant.setPassword(encodePassword);
        return R.operate(merchantService.save(merchant));
    }

    @PutMapping("/update")
    @ApiOperation("根据id更改一个厂商")
    R update(@RequestBody Merchant merchant){
        if(merchantService.updateById(merchant)){
            return R.ok();
        }
        return R.error();
    }

    @DeleteMapping("/removeById/{id}")
    @ApiOperation("根据id删除一个厂商")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要删除的厂商id", defaultValue = "-1"),

    }
    )
    R removeById(@PathVariable Long id) {

        return R.operate (merchantService.removeById(id));
    }
    @GetMapping("/getProductById/{id}")
    @ApiOperation("根据id得到一个产品")
    public Product getProductById(@PathVariable Long id){
        return  productClient.getProductById(id);

    }
    @GetMapping("/getProductList")
    @ApiOperation("得到所有产品")
    public List<Product> getProductList(){
        return  productClient.getProductList();

    }
    @PostMapping("/saveProduct")
    @ApiOperation("新增一个产品")
    public R saveProduct(@RequestBody Product product) {
        return productClient.saveProduct(product);

    }
    @PostMapping("/deleteProductById")
    @ApiOperation("根据id删除一个产品")
    public R deleteProduct(@RequestBody Product product) {
        return productClient.deleteProductById(product);

    }
    @PutMapping("/updateProduct")
    @ApiOperation("根据id更改一个产品")
    public R updateProduct(@RequestBody Product product){
        return productClient.updateProductById(product);
    }
    @GetMapping("/getIndentsByMid/{mid}")
    @ApiOperation("根据uid查询订单")
    public List<Indent> getIndentsByMid(@PathVariable Long mid){
        return  indentClient.getIndentsByMid(mid);

    }

    @PostMapping("/findByNameAndPwd/{name}/{password}")
    @ApiOperation("按用户名或手机号跟密码查询厂商")
    public Merchant findByNameAndPwd(@PathVariable String name,@PathVariable String password) {
        String encodePassword= Base64Utils.encodeToUrlSafeString(password.getBytes());
        QueryWrapper<Merchant> wrapper = new QueryWrapper<>();
//        wrapper.and(x ->
//                x.eq("name", name).eq("password", encodePassword)
//                        .or().eq("phone",name).eq("password", encodePassword)
//        );
        wrapper.eq("password",encodePassword)
                .and(x->x.eq("name",name).or().eq("phone",name));
       return merchantService.getOne(wrapper);

    }

    @GetMapping("/checkExistByName/{name}")
    @ApiOperation("判断厂商的用户名（或者手机号）是否存在")
    public boolean checkExistByName(@PathVariable String name){
        QueryWrapper<Merchant> wrapper = new QueryWrapper<>();
                wrapper.eq("name",name)
                        .or()
                        .eq("phone",name);


        Merchant one;
        one= merchantService.getOne(wrapper);
        if (one!=null){
            return true;
        }
        else{
            return false;
        }

    }



}

